package ncu.me.musicplayer;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import ncu.me.musicplayer.adapter.HistoryAdapter;
import ncu.me.musicplayer.adapter.MusicAdapter;
import ncu.me.musicplayer.entity.MusicInfo;
import ncu.me.musicplayer.entity.SearchItem;
import ncu.me.musicplayer.http.IMusicHttp;
import ncu.me.musicplayer.http.MusicHttp;
import ncu.me.musicplayer.player.MusicManager;
import ncu.me.musicplayer.sql.SearchDao;

public class SearchActivity extends AppCompatActivity {

    Unbinder unbinder;

    @BindView(R.id.music_list)
    RecyclerView recyclerView;

    //搜索歌曲结果适配器
    MusicAdapter adapter;

    //搜索历史适配器
    HistoryAdapter historyAdapter;
    @BindView(R.id.search)
    SearchView searchView;
    MusicHttp http = new MusicHttp();

    public static void startActivity(Activity activity) {
        Intent intent = new Intent();
        intent.setClass(activity, SearchActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        unbinder = ButterKnife.bind(this);
        setTitle("搜索歌曲");

        //展开searchView
        searchView.onActionViewExpanded();
        adapter = new MusicAdapter();
        historyAdapter = new HistoryAdapter();
        recyclerView.setAdapter(historyAdapter);
        historyAdapter.setList(SearchDao.getInstance().query(""));
        historyAdapter.setOnItemClickListener((v,pos)->{
            SearchItem searchItem = historyAdapter.get(pos);
            searchItem.setTime(new Date().getTime());
            SearchDao.getInstance().update(searchItem);
            searchView.setQuery(searchItem.getKey(),true);
        });
        historyAdapter.setOnItemClickListener((view, pos) -> {
            SearchDao.getInstance().delete(historyAdapter.get(pos));
            historyAdapter.getList().remove(pos);
            historyAdapter.notifyDataSetChanged();
        },R.id.item_delete);
        adapter.setOnItemClickListener((view, pos) -> http.download(adapter.get(pos).getMid(), (IMusicHttp.DownloadResult) filePath -> {
            adapter.get(pos).setFilePath(filePath);
            MusicManager.getInstance().add(adapter.get(pos),true);
            finish();
        }));
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                SearchItem searchItem = new SearchItem();
                searchItem.setKey(query);
                searchItem.setTime(new Date().getTime());
                SearchDao.getInstance().insert(searchItem);
                recyclerView.setAdapter(adapter);
                http.search(query, new IMusicHttp.SearchResult() {
                    @Override
                    public void onSuccess(List<MusicInfo> list) {
                        adapter.setList(list);
                        runOnUiThread(()->adapter.notifyDataSetChanged());
                    }
                });
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                if (!(recyclerView.getAdapter() instanceof HistoryAdapter)){
                    recyclerView.setAdapter(historyAdapter);
                }
                historyAdapter.setList(SearchDao.getInstance().query(newText));
                return false;
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }
}